<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<section class="content-header">
    <h1>
        串料申请单列表
    </h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" name="ceoNeedCode" class="form-control"
                                           placeholder="请输入需求项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" name="ceoNeedName" class="form-control"
                                           placeholder="请输入需求项目名称">
                                </div>
                                <div class="form-group">
                                    <label>单据编号</label>
                                    <input type="text" name="ceoCode" class="form-control"
                                           placeholder="请输入单据编号">
                                </div>
                                <div class="form-group">
                                    <label>使用部门</label>
                                    <input type="text" name="ceoNeedDept"
                                           class="form-control"
                                           placeholder="请输入使用部门">
                                </div>
                                <div class="form-group">
                                    <label>制单人</label>
                                    <input type="text" name="ceoBillUser" class="form-control"
                                           placeholder="请输入制单人">
                                </div>
                                <div class="form-group">
                                    <label>
                                        状态
                                    </label>
                                    <select name="ceoStatus" class="form-control">
                                        <option value="2" selected>未执行</option>
                                        <option value="3">已执行</option>
                                        <option value="">全部</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>制单日期</label>
                                    <input type="text" class="form-control datepicker"
                                           name="startTimeQuery"
                                           readonly="readonly"/>
                                    <span>至</span>
                                    <input type="text" class="form-control datepicker"
                                           name="endTimeQuery"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn_search">搜索</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="row" id="toolbar">
                    <div class="form-inline">
                        <button shiro:hasPermission="matinv:matexeccha:edit" type="button"
                                class="btn btn-success"
                                id="btn-togetherCheck">
                            批量执行
                        </button>
                    </div>

                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>

<script th:inline="javascript">

    /** 串料执行（审批） */
    function check(id) {
        js.modal.confirm("确定执行串料申请吗？", function (result) {
            js.post({
                url: ctx + "material/changeOrder/checkSingle",
                data: {
                    ceoId: id
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            })
        })
    }

    /** 串料撤销 */
    function cancel(id) {
        js.modal.confirm("确定撤销被执行串料申请吗？", function (result) {
            js.post({
                url: ctx + "material/changeOrder/cancelSingle",
                data: {
                    ceoId: id
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            })
        })
    }

    /** 更改状态 */
    function updateStatus(id, status) {
        js.modal.confirm("确定驳回串料申请吗？", function (result) {
            js.post({
                url: ctx + "material/changeOrder/updateStatus",
                data: {
                    ceoId: id,
                    ceoStatus: status
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            })
        })
    }

    $(function () {

        table = js.table.init({
            url: ctx + "material/changeOrder/list",
            pageSize: 300,
            queryData: {ceoPageFlgQuery: 2},
            showExport: false,
            columns: [
                {
                    field: "checked",
                    title: '全选',
                    checkbox: true,
                    formatter: function (value, row, index) {
                        if (row.ceoStatus === 2) {
                            return {
                                disabled: false,
                            }
                        } else {
                            return {
                                disabled: true,
                            }
                        }
                    }
                },
                {title: '单据编号', field: 'ceoCode', width: '40'},
                {title: '需求项目编号', field: 'ceoNeedCode'},
                {title: '需求项目名称', field: 'ceoNeedName'},
                {title: '需求物料描述', field: 'ceoNeedMatDesc'},
                {title: '需求重量', field: 'ceoNeedWgt'},
                {title: '使用部门', field: 'ceoNeedDept'},
                {title: '挪用项目编号', field: 'ceoLendCode'},
                {title: '挪用项目名称', field: 'ceoLendName'},
                {title: '挪用物料描述', field: 'ceoLendMatDesc'},
                {title: '制单日期', field: 'ceoBillDate'},
                {title: '申请人', field: 'ceoBillUser'},
                {
                    title: '状态', field: 'ceoStatus',
                    formatter: function (value, row, index) {
                        if (value === 2) {
                            return '<span class="badge bg-yellow">未执行</span> ';
                        } else {
                            return '<span class="badge bg-green">已执行</span> ';
                        }
                    }
                },
                {
                    title: '操作', width: '140',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a target="_blank" class="btn btn-info btn-xs" href="' + ctx + 'material/changeOrder/view/' + row.ceoId + '" >查看</a>');
                        if (hasP("matinv:matexeccha:edit") && (row.ceoStatus === 2)) {
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0);" onclick="check(' + row.ceoId + ')" >执行</a>');
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="updateStatus(' + row.ceoId + ',7)"> 驳回</a>');
                        }
                        if (hasP("matinv:matexeccha:edit") && (row.ceoStatus === 3)) {
                            actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0);" onclick="cancel(' + row.ceoId + ')" >撤销</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 表格搜索 */
        $("#btn_search").click(function () {
            js.table.search(table);
        });

        /** 批量执行 */
        $("#btn-togetherCheck").click(function () {

            var rows = js.table.selectRows("bootstrap-table");

            // 如果没有选择，显示提示信息
            if (rows.length == 0) {
                js.modal.warning("请至少选择一条明细");
                return false;
            } else {
                var arr = js.table.selectColumns(table, "ceoId");
                js.post({
                    url: ctx + "material/changeOrder/checkTogether",
                    contentType: "application/json",
                    data: JSON.stringify(arr),
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            js.table.refresh(table);
                        } else {
                            js.modal.warning(result.msg);
                            js.table.refresh(table);
                        }
                        js.modal.closeLoading();

                    }
                })
            }
        });

    })
</script>
</body>
</html>